package com.goodwilldd.myapp.repository;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

import com.goodwilldd.myapp.entities.Person;
import com.goodwilldd.myapp.entities.Person_;

public class PersonSpecifications {
	public static Specification<Person> lastNameIsLike(final String searchTerm) {

		return new Specification<Person>() {
			@Override
			public Predicate toPredicate(Root<Person> personRoot,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				String likePattern = getLikePattern(searchTerm);
				return cb.like(
						cb.lower(personRoot.<String> get(Person_.lastName)),
						likePattern);
			}

			private String getLikePattern(final String searchTerm) {
				StringBuilder pattern = new StringBuilder();
				pattern.append(searchTerm.toLowerCase());
				pattern.append("%");
				return pattern.toString();
			}
		};
	}
}
